package com.example.demoyyy.common;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.example.demoyyy.ShiTiYYY.Zhifubao;
import org.springframework.stereotype.Component;

@Component//注解 做成一个组件类
public class Alipay {

    public String pay(Zhifubao zhifubao) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        // 创建配置对象
        String privateKey = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCczlKtkh3gqX/JHATyzz0jZae4ikedOjUsdKn5fxy3MfloQI1f7qPejZmu/KRCs8lgzsyq+2qwEsJTnZIpnBzB/X7eMIYQqZzy2jlIEBOtlX2Rl8FJ8nTQMxTrnJsttwXbjhmL/8yNa8XbUXA5W8LcyR0JpAFIFC5S9N/fZrvPd0qOm0qiCAALPLhtUoq6CMydBw6kc6r1TWbvYT3FLK6iA9k6Tnw0NWN7yNIvcrCGu4Saa0jiGKttD9Zx0vjGusemy7uAxAEghF+TwdNbof61aNOC8l1KFApYLQu25OuybYaO/bpphNAZTA7SfCjdeT1EaKRtbKaFoZusTH1lRxvnAgMBAAECggEBAJcC0HbeMC3rruf0NwPL93o+bR749O0DYLA2jnd8JiB7XVE8UaqgvWoYfngaUOr4EClOH6dOx7lX55P1PQ0bjkp4ohTDMGmdn0v2Q5IWAY17WVm8h2Z6vGrBtxUNVhjZmSePmyTLrahIsVx8+pISwAA3IuLk8mMDJHW55FxWqOqr0Rhuu2bg9F2dWMGDHgYDW6Wh82JrTs8JUt7HIBqeYMjS1JGWtyVPSh3GP6ZY1m3+vf9pM9ZcgbDFqfCQNuo5HVOYIfHpGNAo9t8kLvX+OkujQg+W4/fQ1Ojxd/m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GKbjGk+nFe4i4xX/08uz4elpxbigP2SwTcZx9P+rx1LH8XW3gqbeYGSehOC4pEvzeAOOPZl6cl8evbpxtRL3zgxUTBmU7EKuRPR9SSCVa5gTcmztgsppYHONmfNsW5jiCeHTOpNnHxa+9HkrcdWO0I6lgaQOATatyqaZypELT+h1K2ESRkgHyIzBUd+EFK+gpXOSFS1dshm0/6Qo7wQ9Y4R9YAIj3HUkxmEMmyx66xXbHmdSVoi6suKH6LzBDwIDAQAB"; // 自己的公钥
        String APPID = "9021000152684319"; // 自己的应用编号
        String returnUrl = "http://120.26.241.28:8081/front/page/pay-success.html";
        // 支付成功后跳转的页面
        String serverUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
        // 支付宝网关地址

        alipayConfig.setServerUrl(serverUrl);
        alipayConfig.setAppId(APPID);
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        // 设置这些数据

        alipayConfig.setFormat("json");// 设置格式
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");

        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        // 创建支付客户端对象
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        // 创建手机浏览器请求对象,用来请求打开支付宝app
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        // 手机浏览器支付 ,拉起支付宝app

        model.setOutTradeNo(zhifubao.getOut_trade_no());// 订单编号
        model.setTotalAmount(zhifubao.getTotal_amount()); // 订单金额
        model.setSubject(zhifubao.getSubject());// 订单主题
        model.setProductCode("QUICK_WAP_WAY"); // 产品编号,手机端浏览器支付,固定,不需要修改
        model.setSellerId("nwfnla7749@sandbox.com"); // 设置成自己的商家支付宝账号
        // 注意:一定要用用商家的支付宝账号,在沙箱中的商家信息中
        request.setBizModel(model);//将数据设置到请求对象中
        request.setReturnUrl(returnUrl); // 设置支付成功之后跳转的页面

        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request);
        // 发起支付请求
        String result = response.getBody();
        // 返回支付后的响应结果
        return result;
    }
}
